package com.hspedu.homework.Homework01;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * @author DL5O
 * @version 1.0
 */
public class Client {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("连接上服务端...");
        Scanner sc = new Scanner(System.in);
        String info = "";
        String data = "";

        while (true) {
            //先写
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            info = sc.next();
            System.out.println(info);
            bw.write(info);
            bw.flush();
            bw.close();
            socket.shutdownOutput();
            if (info.equals("quit")) {
                bw.close();
                break;
            }
            //写完之后再读
            //读取服务端发的消息
            data = StreamUtils.streamToString(socket.getInputStream());
            System.out.println(data);
            socket.shutdownInput();
        }
        socket.shutdownOutput();
        socket.close();
        System.out.println("客户端退出...");
    }
}
